#设置CMake最低支持版本
cmake_minimum_required(VERSION 3.17)


set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)


#设置C标准版本
set(CMAKE_C_STANDARD 99)

#设置C++标准版本
set(CMAKE_CXX_STANDARD 11)

ENABLE_LANGUAGE(ASM)
set(CROSS_COMPILER_PATH "S:/softWareInstall/MounRiver/MounRiver_Studio/toolchain/arm-none-eabi-gcc/bin")

set(CMAKE_C_COMPILER "${CROSS_COMPILER_PATH}/arm-none-eabi-gcc.exe")
#set(CMAKE_ASM_COMPILER "${CROSS_COMPILER_PATH}/arm-none-eabi-gcc.exe")
set(CMAKE_CXX_COMPILER "${CROSS_COMPILER_PATH}/arm-none-eabi-g++.exe")
set(CMAKE_OBJCOPY "${CROSS_COMPILER_PATH}/arm-none-eabi-objcopy.exe")
set(CMAKE_OBJDUMP "${CROSS_COMPILER_PATH}/arm-none-eabi-objdump" )
set(CMAKE_SIZE "${CROSS_COMPILER_PATH}/arm-none-eabi-size" )
set(CMAKE_NM "${CROSS_COMPILER_PATH}/arm-none-eabi-nm" )
set(CMAKE_DEBUGER "${CROSS_COMPILER_PATH}/arm-none-eabi-gdb")
set(CMAKE_CPPFILT "${CROSS_COMPILER_PATH}/arm-none-eabi-c++filt" )
set(CMAKE_FIND_ROOT_PATH "${CROSS_COMPILER_PATH}")


set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

#set(CMAKE_C_FLAGS "-mcpu=cortex-m3 -mthumb -mno-thumb-interwork -ffunction-sections -fdata-sections")
#set(CMAKE_ASM_FLAGS -march=armv5te)

set(LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/LD/CH32F203RCT6.ld)
find_program(ARM_SIZE_EXECUTABLE ${CROSS_COMPILER_PATH}/arm-none-eabi-size.exe)
find_program(ARM_GDB_EXECUTABLE ${CROSS_COMPILER_PATH}/arm-none-eabi-gdb.exe)
find_program(ARM_OBJCOPY_EXECUTABLE ${CROSS_COMPILER_PATH}/arm-none-eabi-objcopy.exe)
find_program(ARM_OBJDUMP_EXECUTABLE ${CROSS_COMPILER_PATH}/arm-none-eabi-objdump.exe)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
#set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-lc -lm -lnosys")

#message("oldwang ${CMAKE_SOURCE_DIR}/LD/CH32F203RCT6.ld")

set(CMAKE_C_COMPILER_WORKS TRUE)
set(CMAKE_CXX_COMPILER_WORKS TRUE)
#设置工程名

set(MCU_FLAGS "-mcpu=cortex-m3 -mthumb -mthumb-interwork ")
# compiler: language specific flags CFLAGS
set(CMAKE_C_FLAGS "${MCU_FLAGS} -std=gnu99 -Wall -fdata-sections -ffunction-sections -O3")
#CPP
set(CMAKE_CXX_FLAGS "${MCU_FLAGS} -fno-rtti -fno-exceptions -fno-builtin -Wall -fdata-sections -ffunction-sections" )
#ASFLAGS
set(CMAKE_ASM_FLAGS "${MCU_FLAGS} -x assembler-with-cpp")
#LDFLAGS -mcpu=cortex-m0plus -mthumb
set(CMAKE_EXE_LINKER_FLAGS "${MCU_FLAGS} -T${LINKER_SCRIPT} -nostartfiles -Xlinker --gc-sections -Wl,-Map,oldwang_CH32F203RCT6.map --specs=nano.specs --specs=nosys.specs")
#要链接的库 对应makefile的 LIBS
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-lc -lm -lnosys")






project(ch32f203 C CXX ASM)
#添加全局宏定义
#add_definitions(-DGD32F30X_HD -DUSE_STDPERIPH_DRIVER)

#添加头文件引用目录
include_directories(
        Core
        Debug
        Peripheral/inc
        User
        User/dispaly
        User/dispaly/GUI
        User/dispaly/GUI/lv_examples
        User/dispaly/GUI/lvgl
        User/dispaly/GUI/lvgl_driver
#        "FreeRTOS/"
#        "FreeRTOS/Source/include/"
#        "FreeRTOS/Source/portable/Common/"
#        "FreeRTOS/Source/portable/MemMang/"
#        "FreeRTOS/Source/portable/RVDS/ARM_CM3/"
)
#set_property(SOURCE /Startup/startup_ch32f20x_D8.s PROPERTY LANGUAGE C)
#添加源文件
file(GLOB_RECURSE SOURCES
        "User/*.c"
        "Peripheral/src/*.*"
        "Core/*.*"
        "Startup/startup_ch32f20x_D8.s"
        "Debug/*.*"
        User/dispaly/*.*
        User/dispaly/GUI/*.*
        User/dispaly/GUI/lv_examples/*.*
        User/dispaly/GUI/lvgl/*.*
        User/dispaly/GUI/lvgl_driver/*.*
#        "FreeRTOS/FreeRTOSConfig.h"
#        "FreeRTOS/Source/*.*"
#        "FreeRTOS/Source/include/*.*"
#        "FreeRTOS/Source/portable/Common/*.*"
#        "FreeRTOS/Source/portable/MemMang/heap_4.c"
#        "FreeRTOS/Source/portable/RVDS/ARM_CM3/*.*"
        )

#添加生成可执行文件
add_executable(${PROJECT_NAME}.elf
        ${SOURCES}
        User/Main.c
)

set(ELF_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf)
set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)


add_custom_command(
        TARGET "${PROJECT_NAME}.elf" POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -Obinary ${ELF_FILE} ${BIN_FILE}
        COMMAND ${CMAKE_OBJCOPY} -Oihex ${ELF_FILE} ${HEX_FILE}
        COMMENT "Building ${PROJECT_NAME}.bin and ${PROJECT_NAME}.hex"
        COMMAND ${CMAKE_COMMAND} -E copy ${HEX_FILE} "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.hex"
        COMMAND ${CMAKE_COMMAND} -E copy ${BIN_FILE} "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.bin"
        COMMAND ${CMAKE_SIZE} --format=berkeley ${PROJECT_NAME}.elf ${PROJECT_NAME}.hex
        COMMENT "Building Ok!"
        )




